package com.fh.modules.system.controller;


import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.constant.Constant;
import com.fh.common.util.ConvertUtils;
import com.fh.common.web.AbstractController;
import com.fh.modules.system.entity.SysDictItem;
import com.fh.modules.system.service.ISysDictItemService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


/**
 * @author Qiu,Junda
 * @date 2019年4月3日
 * @description 前端控制器
 */
@RestController
@RequestMapping("/sys/dictItem")
@Api("字典属性值")
public class SysDictItemController extends AbstractController{

	@Autowired
	private ISysDictItemService sysDictItemService;
	
	/**
	 * @功能：查询字典数据
	 * @param sysDictItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ApiOperation("字典属性分页查询")
	@AutoLog(value="字典属性分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result<IPage<SysDictItem>> queryPageList(SysDictItem sysDictItem,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) {
		Result<IPage<SysDictItem>> result = new Result<IPage<SysDictItem>>();
		sysDictItem.setDelFlag(0);
		QueryWrapper<SysDictItem> queryWrapper = new QueryWrapper<SysDictItem>(sysDictItem);
		Page<SysDictItem> page = new Page<SysDictItem>(pageNo,pageSize);
		//排序逻辑 处理
		String column = req.getParameter("column");
		String order = req.getParameter("order");
		if(ConvertUtils.isNotEmpty(column) && ConvertUtils.isNotEmpty(order)) {
			if(Constant.ORDER_ASC.equals(order)) {
				queryWrapper.orderByAsc(ConvertUtils.camelToUnderline(column));
			}else {
				queryWrapper.orderByDesc(ConvertUtils.camelToUnderline(column));
			}
		}
		IPage<SysDictItem> pageList = sysDictItemService.page(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}
	
	/**
	 * @功能：新增
	 * @return
	 */
	@ApiOperation("新增字典属性")
	@AutoLog("新增字典属性")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result<SysDictItem> add(@RequestBody SysDictItem sysDictItem) {
		Result<SysDictItem> result = new Result<SysDictItem>();
		sysDictItem.setCreateTime(new Date());
		sysDictItemService.save(sysDictItem);
		result.success("保存成功！");
		return result;
	}
	
	/**
	 * @功能：编辑
	 * @param sysDictItem
	 * @return
	 */
	@ApiOperation("修改字典属性")
	@AutoLog("修改字典属性")
	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
	public Result<SysDictItem> edit(@RequestBody SysDictItem sysDictItem) {
		Result<SysDictItem> result = new Result<SysDictItem>();
		SysDictItem sysdict = sysDictItemService.getById(sysDictItem.getId());
		if(sysdict==null) {
			result.error500("未找到对应实体");
		}else {
			sysDictItem.setUpdateTime(new Date());
			boolean ok = sysDictItemService.updateById(sysDictItem);
			if(ok) {
				result.success("编辑成功!");
			}
		}
		return result;
	}
	
	/**
	 * @功能：删除字典数据
	 * @param id
	 * @return
	 */
	@ApiOperation("删除字典属性")
	@AutoLog("删除字典属性")
	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	public Result<SysDictItem> delete(@RequestParam(name="id",required=true) String id) {
		Result<SysDictItem> result = new Result<SysDictItem>();
		SysDictItem joinSystem = sysDictItemService.getById(id);
		if(joinSystem==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = sysDictItemService.removeById(id);
			if(ok) {
				result.success("删除成功!");
			}
		}
		return result;
	}
	
	/**
	 * @功能：批量删除字典数据
	 * @param ids
	 * @return
	 */
	@ApiOperation("批量删除字典属性")
	@AutoLog("批量删除字典属性")
	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	public Result<SysDictItem> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		Result<SysDictItem> result = new Result<SysDictItem>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.sysDictItemService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
}
